package com.cdhong.shop.backend.entity;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseEntity {
    private int code;  // 0 正常  1 错误   2 请先登录

    private enum CODE{
        SUCCESS(0),
        FAIL(1),
        NEED_LOGIN(2);

        final Integer code;
        CODE(Integer code){
            this.code = code;
        }
    }

    private String msg;
    private Object data;

    public static ResponseEntity ok(String msg) {
        return ResponseEntity.builder().code(CODE.SUCCESS.code).msg(msg).build();
    }

    public static ResponseEntity ok(String msg,String  token) {
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        return ResponseEntity.builder().code(CODE.SUCCESS.code).msg(msg).data(map).build();
    }

    public static ResponseEntity error(String msg) {
        return ResponseEntity.builder().code(CODE.FAIL.code).msg(msg).build();
    }

    public static ResponseEntity data(Object obj) {
        return ResponseEntity.builder().code(CODE.SUCCESS.code).data(obj).build();
    }

    public static ResponseEntity data(String msg,Object obj) {
        return ResponseEntity.builder().code(CODE.SUCCESS.code).msg(msg).data(obj).build();
    }

    public static ResponseEntity page(long total, Object obj) {
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("list", obj);
        return ResponseEntity.builder().code(CODE.SUCCESS.code).data(data).build();
    }

}